package JZOffer;

import java.util.ArrayDeque;
import java.util.Deque;

// 从尾到头打印链表
public class JzOffer06 {
    public int[] reversePrint(ListNode head) {
        if (head == null) {
            return new int[0];
        }
        ListNode cur = head;
        Deque<ListNode> stack = new ArrayDeque<>();

        while (cur != null) {
            stack.push(cur);
            cur = cur.next;
        }
        int[] res = new int[stack.size()];

        for (int i = 0; i < res.length; i++) {
            res[i] = stack.pop().val;
        }
        return res;
    }
}
